﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Web.Services;
using DotNetNuke.Modules.AjaxWebLinks.Components;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace DotNetNuke.Modules.AjaxWebLinks.Services
{

    public class HandlerController : DnnController
    {
        private AjaxWebLinksController _controller;
        private const string URL_PATTERN = @"^https?://([\w]+\.)?[\w]+\.[\w/:.?=%]+$";
        private ServiceSecurity _securityContext;

        protected override void  Initialize(System.Web.Routing.RequestContext requestContext)
        {
 	         base.Initialize(requestContext);
    
            _controller = new AjaxWebLinksController();
            if (ActiveModule != null)
            {
                _securityContext = new ServiceSecurity(ActiveModule.ModuleID, ActiveModule.TabID);
            }
           
        }

        [DnnAuthorize(AllowAnonymous=true)]
        public string HelloWorld()
        {
            
            return "Hello World!";
        }

        [DnnAuthorize(AllowAnonymous=true)]
        public JsonResult GetLinks()
        {
            return Json( _controller.GetLinks(ActiveModule.ModuleID));
        }

        [DnnAuthorize(RequiresHost=false)]
        public JsonResult GetLink(int LinkID)
        {
            return Json(_controller.GetLink(ActiveModule.ModuleID, LinkID));
        }

        [DnnAuthorize(RequiresHost=false)]
        public JsonResult AddLink(string LinkURL, string LinkText)
        {
            var r = new ResponseWrapper();
            LinkURL = LinkURL.Trim();

            //Would be nice to create an attribute to assert permissions [access(level= accesslevel.edit)
            if (!_securityContext.CanEdit())
            {

                r.Error = "Permission Denied";
                return Json(r);
            }

            var regex = new Regex(URL_PATTERN);

            if (!regex.IsMatch(LinkURL))
            {
                r.Error = "Invalid link format";
                return Json(r);
            }

            // var scriptRegex = new Regex(SCRIPT_PATTERN);

            if (LinkText.Length == 0)
            {
                r.Error = "Invalid Link text";
                return Json(r);
            }

            //returns the id of the new link or 0 if failed

            var link = new AjaxWebLinkInfo();
            link.LinkText = LinkText;
            link.LinkURL = LinkURL;
            link.ModuleId = ActiveModule.ModuleID;

            r.Data = _controller.AddLink(link);
            return Json(r);

        }

        [DnnAuthorize(RequiresHost=false)]
        public JsonResult DeleteLink(int LinkID)
        {
            var r = new ResponseWrapper();

            if (!_securityContext.CanEdit())
            {
                r.Error = "Permission Denied";
                return Json(r);
            }


            //returns the id of the new link or 0 if failed
            _controller.DeleteLink(ActiveModule.ModuleID, LinkID);

            r.Data = true;

            return Json(r);
        }

        [DnnAuthorize(RequiresHost=false)]
        public JsonResult UpdateLink(int LinkID, string LinkText, string LinkURL)
        {
            var r = new ResponseWrapper();
            LinkURL = LinkURL.Trim();

            if (!_securityContext.CanEdit())
            {
                r.Error = "Permission Denied";
                return Json(r);
            }

            var regex = new Regex(URL_PATTERN);

            if (!regex.IsMatch(LinkURL))
            {
                r.Error = "Invalid link format";
                return Json(r);
            }

            //  var scriptRegex = new Regex(SCRIPT_PATTERN);

            if (LinkText.Length == 0)
            {
                r.Error = "Invalid Link text";
                return Json(r);
            }

            var link = _controller.GetLink(ActiveModule.ModuleID, LinkID);
            if (link != null)
            {
                link.LinkText = LinkText;
                link.LinkURL = LinkURL;
                _controller.UpdateLink(link);
                r.Data = true;
            }

            //TODO: add try catch and other logic to verify update took place

            return Json(r);
        }



    }
}